package com.jzy.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.jzy.dao.PermissionDao;
import com.jzy.dao.RoleDao;
import com.jzy.dao.UserDao;
import com.jzy.pojo.Permission;
import com.jzy.pojo.Role;
import com.jzy.pojo.User;
import com.jzy.service.OrderSettingService;
import com.jzy.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Set;

@org.springframework.stereotype.Service // Spring的
@com.alibaba.dubbo.config.annotation.Service(interfaceClass = UserService.class) // Dubbo的
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private PermissionDao permissionDao;

    //根据用户名查询数据库获取用户信息和关联的角色信息，同时需要查询角色关联的权限信息。
    public User findByUsername(String username) {
        User user = userDao.findByUsername(username); //查询用户基基本信息，不包含用户的角色
        if (user == null) {
            return null;
        }
        Integer userId = user.getId();
        //根据用户ID查询对应的角色
        Set<Role> roles = roleDao.findByUserId(userId);
        if (roles != null && roles.size() > 0) {
            for (Role role : roles) {
                Integer roleId = role.getId();
                //根据角色ID查询关联的权限
                Set<Permission> permissions = permissionDao.findByRoleId(roleId);
                if (permissions != null && permissions.size() > 0) {
                    role.setPermissions(permissions);//让角色关联权限
                }
            }
            user.setRoles(roles);//让用户关联角色
        }
        return user;
    }
}